package com.iseastar.guojiang.loc;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.iseastar.guojiang.util.MediaPlayerUtils;

import droid.frame.utils.android.Log;
import droid.frame.utils.android.Notify;
import droid.frame.utils.android.VibratorMgr;

/**
 * Created by joker on 2018/4/2.
 * 火箭侠应用退到后台后，启动service进行新任务铃声提醒和震动
 */
public class AppBackgroundService extends Service implements MediaPlayer.OnCompletionListener {

    //    private MediaPlayer mediaPlayer = null;
//    private PlayerSoundAsync playerSoundAsync = null;
    private ServiceTaskBean serviceTaskBean = null;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Notify.getInstance().startForeground(this);
    }

    @SuppressLint("WrongConstant")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        flags = START_STICKY;
        updateData();
        return super.onStartCommand(intent, flags, startId);
    }

    private void updateData() {
        if (AppLogic.getAppBackgroundPassportList() != null && AppLogic.getAppBackgroundPassportList().size() > 0) {
//            playerSoundAsync = null;
//            if (mediaPlayer == null) {
//                mediaPlayer = new MediaPlayer();
//            }
//            if (mediaPlayer.isPlaying()) {
//                mediaPlayer.stop();
//                mediaPlayer.reset();
//            }
//            mediaPlayer.setOnCompletionListener(this);
            for (ServiceTaskBean bean : AppLogic.getAppBackgroundPassportList()) {
                Log.d("33333", "appBackgroundList：" + bean.getTaskId() + "，任务名：" + bean.getBuildName());
            }
            for (ServiceTaskBean bean : AppLogic.getPassportList()) {
                Log.d("33333", "passportList：" + bean.getTaskId() + "，任务名：" + bean.getBuildName());
            }
            serviceTaskBean = AppLogic.getAppBackgroundPassportList().get(0);
            //取消任务
            if (serviceTaskBean.getPassportType() == 1) {
                MediaPlayerUtils.getInstance().playerMusic("courier_cancel_task.mp3", false);
            } else {
                MediaPlayerUtils.getInstance().playerMusic("courier_new_task.mp3", false);
            }
            MediaPlayerUtils.getInstance().getMediaPlayer().setOnCompletionListener(this);
            long[] pattern = {100, 400}; // 停止 开启 停止 开启
            VibratorMgr.vibrate(pattern, 0);
//            playerSoundAsync = new PlayerSoundAsync();
//            playerSoundAsync.execute();
        }
    }

    /**
     * mediaPlayer.start();//开始或恢复播放
     * mediaPlayer.pause();//暂停播放
     * mediaPlayer.start();//恢复播放
     * mediaPlayer.stop();//停止播放
     * mediaPlayer.release();//释放资源
     */
//    class PlayerSoundAsync extends AsyncTask<Void, Void, Void> {
//
//        @Override
//        protected void onPreExecute() {
//
//        }
//
//        @Override
//        protected Void doInBackground(Void... voids) {
//            try {
//                AssetFileDescriptor fileDescriptor = null;
//                //取消任务
//                if (serviceTaskBean.getPassportType() == 1) {
//                    fileDescriptor = BeeApplication.getContext().getAssets().openFd("courier_cancel_task.mp3");
//                } else {
//                    fileDescriptor = BeeApplication.getContext().getAssets().openFd("courier_new_task.mp3");
//                }
//                if (fileDescriptor == null) {
//                    return null;
//                }
//                Log.d("33333", "doInBackground():" + serviceTaskBean.getPassportType());
//                mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
//                        fileDescriptor.getStartOffset(),
//                        fileDescriptor.getLength());
//                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//STREAM_ALARM
//                mediaPlayer.prepare();
//                mediaPlayer.setVolume(1.0f, 1.0f);
//                mediaPlayer.start();
//            } catch (Exception e) {
//                Log.d("33333", "doInBackground():Exception");
//                e.printStackTrace();
//            }
//            long[] pattern = {100, 400}; // 停止 开启 停止 开启
//            VibratorMgr.vibrate(pattern, 0);
//            return null;
//        }
//    }
    @Override
    public void onCompletion(MediaPlayer mp) {
        if (MediaPlayerUtils.getInstance().getMediaPlayer() != null) {
            MediaPlayerUtils.getInstance().destoryPlayer();
        }
        VibratorMgr.cancelVibrator();
        if (AppLogic.getAppBackgroundPassportList() != null && AppLogic.getAppBackgroundPassportList().size() > 0) {
            AppLogic.getAppBackgroundPassportList().remove(0);
            if (AppLogic.getAppBackgroundPassportList().size() <= 0) {
                MediaPlayerUtils.getInstance().playerMusic("courier_silence.mp3", true);
            } else {
                updateData();
            }
        } else {
            MediaPlayerUtils.getInstance().playerMusic("courier_silence.mp3", true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        if (mediaPlayer != null) {
//            mediaPlayer = null;
//        }
        VibratorMgr.cancelVibrator();
//        if (playerSoundAsync != null) {
//            playerSoundAsync = null;
//        }
    }
}
